package org.example.struct.array;

/**
 * @author liyishan
 * @date 2024/2/2 10:22
 * @apiNote
 */

public class Test01556ThousandSeparator {
    public static void main(String[] args) {
        System.out.println(thousandSeparator1(0));
    }

    public static String thousandSeparator(int n) {
        String s = String.valueOf(n);
        return s.replaceAll("(\\d)(?=(\\d{3})+$)", "$1.");
    }

    public static String thousandSeparator1(int n) {
        String s = String.valueOf(n);
        StringBuffer sb = new StringBuffer();
        int size = s.length() % 3;
        if (size != 0) {
            if (s.length() < 3) {
                sb.append(s, 0, size);
            } else {
                sb.append(s, 0, size).append(".");
            }
        }
        int count = 0;
        for (int i = size; i < s.length(); i++) {
            count++;
            sb.append(s.charAt(i));
            if (count == 3 && i != s.length() - 1) {
                sb.append(".");
                count = 0;
            }
        }
        return sb.toString();
    }
}
